import xml.etree.ElementTree as et
from app.functions.funcs import formate_time
class rawData:
    """ parsing raw xml data from xml string """
    def __init__(self,xmlstring=None):
        self.tree = et.fromstring(xmlstring)

    def getEventStart(self):
        try:
            start = self.tree.find('start').text
        except AttributeError:
            start = None
        setattr(self,'start',formate_time(start))

    def getEventEnd(self):
        try:
            end = self.tree.find('end').text
        except AttributeError:
            end = None
        setattr(self,'end',formate_time(end))

    def getEventRealm(self):
        try:
            realm = self.tree.find('realm').text
        except AttributeError:
            realm = None
        setattr(self,'realm',formate_time(realm))

    def getPlayersInfo(self):
        players = self.tree.find('PlayerInfos').getchildren()
        player_detail = []
        for player in players:
            try:
                player_name=player.findtext('name')
            except AttributeError:
                player_name=None
            try:
                player_race=player.findtext('race')
                player_race = rerace(player_race)
            except AttributeError:
                player_race = "0"
            try:
                player_level=player.findtext('level')
            except AttributeError:
                player_level="70"
            try:
                player_sex=player.findtext('sex')
                player_sex=regender(player_sex)
            except AttributeError:
                player_sex= "0"
            try:
                player_class = player.findtext('class')
                player_class = reclass(player_class)
            except AttributeError:
                player_class= "0"
            try:
                player_guild=player.findtext('guild')
            except AttributeError:
                player_guild=None
            player_dict = {
                            'player_name':player_name,
                            'player_race':player_race,
                            'player_level':player_level,
                            'player_sex':player_sex,
                            'player_class':player_class,
                            'player_guild':player_guild
                            }
            player_detail.append(player_dict)
        setattr(self,'players',player_detail)
    
    def getBossInfo(self):
        boss = self.tree.find('BossKills').getchildren()
        boss_detail = []
        for bo in boss:
            try:
                boss_name = bo.findtext('name')
            except AttributeError:
                boss_name = None
            try:
                boss_score = int(bo.find('score').text)
            except AttributeError:
                boss_score = 0
            boss_detail.append({'boss_name':boss_name,'boss_score':boss_score})
        setattr(self,'boss',boss_detail)
    
    def getLootInfo(self):
        loots = self.tree.find('Loot').getchildren()
        loots_detail = []
        for loot in loots:
            try:
                loot_name=loot.findtext('ItemName')
            except AttributeError:
                loot_name=None
            try:
                loot_id=loot.findtext('ItemID')
            except AttributeError:
                loot_id=None
            try:
                loot_color=loot.find('Color').text
            except AttributeError:
                loot_color="#999"
            try:
                loot_icon=loot.findtext('Icon')
            except AttributeError:
                loot_icon=None
            try:
                loot_player=loot.findtext('Player')
            except AttributeError:
                loot_player=None
            try:
                loot_costs = loot.find('Costs').text
            except:
                loot_costs=0
            try:
                loot_count=loot.find('Count').text
            except:
                loot_count=0
            loots_dict = {
                            'loot_name':loot_name,
                            'loot_id':loot_id,
                            'loot_color':loot_color,
                            'loot_icon':loot_icon,
                            'loot_player':loot_player,
                            'loot_costs':int(loot_costs),
                            'loot_count':int(loot_count)
                            }
            loots_detail.append(loots_dict)
        setattr(self,'loots',loots_detail)

def rerace(player_race):
    if player_race == "Human":
        player_value = "1"
    elif player_race == "Gnome":
        player_value = "7"
    elif player_race == "Draenei":
        player_value = "11"
    elif player_race == "NightElf":
        player_value = "4"
    elif player_race == "Dwarf":
        player_value = "3"
    elif player_race == "Orc":
        player_value = "2"
    elif player_race == "Undead":
        player_value = "5"
    elif player_race == "Troll":
        player_value = "8"
    elif player_race == "Tauren":
        player_value = "6"
    elif player_race == "BloodElf":
        player_value = "10"
    else:
        player_value = "0"
    return player_value

def reclass(player_class):
    if player_class == "WARLOCK":
        player_value = "9"
    elif player_class == "PRIEST":
        player_value = "5"
    elif player_class == "MAGE":
        player_value = "8"
    elif player_class == "DRUID":
        player_value = "11"
    elif player_class == "ROGUE":
        player_value = "4"
    elif player_class == "SHAMAN":
        player_value = "7"
    elif player_class == "HUNTER":
        player_value = "3"
    elif player_class == "PALADIN":
        player_value = "2"
    elif player_class == "WARRIOR":
        player_value = "1"
    elif player_class == "DEATHKNIGHT":
        player_value = "6"
    else:
        player_value = "0"
    return player_value

def regender(player_sex):
    if player_sex == "3":
        player_value = "1"
    elif player_sex == "2":
        player_value = "0"
    else:
        player_value = "0"
    return player_value